package com.zone.baseproject.base;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.zhy.autolayout.AutoLayoutActivity;
import com.zone.baseproject.R;
import com.zone.baseproject.utils.T;


/**
 * Created by john on 2016/7/26.
 */
public abstract class BaseActivity2 extends AutoLayoutActivity implements IBaseView {
    private boolean isShowToast = true;
    private boolean isShowLog = true;
    private AlertDialog dialog;
    private ProgressDialog mProgressDialog = null;

    public abstract int setLayoutId();

    public abstract void init();//初始化所有数据

    public abstract void business();//这个方法中进行所有事务操作。例如：调用对象的方法，设置点击事件等

    public abstract PBase getPresenter();

    public boolean isShowToast() {
        return isShowToast;
    }

    public void setShowToast(boolean showToast) {
        isShowToast = showToast;
    }

    public boolean isShowLog() {
        return isShowLog;
    }

    public void setShowLog(boolean showLog) {
        isShowLog = showLog;
    }

    public abstract boolean isUseToolbar();

    public interface Convert {
        void transaction(View dialogView, AlertDialog alertDialog);
    }

    public interface DialogOnClickListener {
        void positiveListener(DialogInterface dialog, int which);

        void negativeListener(DialogInterface dialog, int which);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutId());
//        ButterKnife.inject(this);
        if (isUseToolbar()) {
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            iniBase();
        }
        init();
        getPresenter().attachView(this);
        business();
    }

    protected void iniBase() {
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//创建返回键，并实现打开关/闭监听
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏系统默认的Title
    }

    protected void backhome(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
    }

    public void showProgressDialog(String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
        }
        mProgressDialog.setMessage(message);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }


    public void hideProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.hide();
        }
    }


    public void showAlertDialog(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }


    public void showToast(String message) {
        if (isShowToast()) {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onResume() {
//        是否横屏
//        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (getPresenter() != null) {
            getPresenter().detachView();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    public void toNextActivity(Class c) {
        startActivity(new Intent(this, c));
    }
}
